<template>
  <el-dialog :visible="dialogVisible"  width="430px" title="移入私海" :close-on-click-modal="false" center>
    <el-form :model="form" ref="form" label-width="90px" style="margin-top: 30px" :rules="rules">
      <el-form-item label="选择分组:" prop="groupId">
        <el-cascader size="small" style="width: 73%"
          :options="groupOptions"
          v-model="form.groupId"
          :show-all-levels="false"
          :props="{ checkStrictly: true, value: 'id', label: 'text', children: 'children'}"
          clearable></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" align="right">
      <el-button @click="dialogVisible=false" size="small">取消</el-button>
      <el-button type="primary" @click="submit" size="small" :disabled="loading" :loading="loading">{{loading?'保存中':'确 定'}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryCustType,movePrivateSea} from '@/api/customer'
    export default {
        name: "DialogMovePerson",
        data(){
            return{
                groupOptions:[],
                form:{
                    custid:'',
                    groupId:[],
                },
                rules: {
                    groupId: [
                        {required: true, message: '请先选择要移入的分组', trigger: 'blur'}
                    ],
                },
                loading:false,
                dialogVisible:false
            }
        },
        methods:{
            init(custid){
                this.form.custid=custid;
                this.dialogVisible=true;
                if(this.groupOptions.length==0){
                    this.getGroup();
                }
            },
            getGroup(){
                queryCustType().then(res => { // 当前用户的分组
                    if(res.data.success){
                        this.groupOptions = res.data.data
                        console.log( this.groupOptions)
                    }
                })
            },
            //提交请求
            submit(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.loading=true;
                        const groupId = this.form.groupId[this.form.groupId.length - 1];
                        movePrivateSea(groupId, this.form.custid).then(res => {
                            this.loading=false;
                            if(res.data.success) {
                                this.$message({
                                    type:'success',
                                    message: res.data.msg
                                })
                                this.dialogVisible = false;
                                this.$emit('relationCustomer')
                            }
                        }).catch(err=>{
                            this.loading=false;
                        })
                    } else {
                        return false;
                    }
                });


            }
        }
    }
</script>

<style scoped>

</style>
